﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Scribd.Net.Responses
{
    public sealed class GetCategories : Response
    {
        public GetCategories(string xml) : base(xml) { }
    
        /// <summary>
        /// Categories.
        /// </summary>
        public List<Category> Categories
        {
            get
            {
                List<Category> _result = new List<Category>();

                // Parse the response
                if (this.HasChildNodes && this.ErrorList.Count < 1)
                {
                    XmlNode _node = this.SelectSingleNode("rsp");

                    XmlNode _set = _node.SelectSingleNode("result_set");

                    foreach (XmlNode _r in _node.SelectNodes("result"))
                    {
                        Category _item = new Category();

                        _item.ID = int.Parse(_r.SelectSingleNode("id").InnerText);
                        _item.Name = _r.SelectSingleNode("name").InnerText.Trim();

                        if (_r.SelectSingleNode("subcategories") != null)
                        {
                            foreach (XmlNode _c in _r.SelectSingleNode("subcategories"))
                            {
                                Category _subItem = new Category();

                                _subItem.ID = int.Parse(_r.SelectSingleNode("id").InnerText);
                                _subItem.Name = _r.SelectSingleNode("name").InnerText.Trim();

                                _item.Subcategories.Add(_subItem);
                            }
                        }

                        _result.Add(_item);
                    }
                }
                return _result;
            }
        }

    }
}
